﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Service = SLWebApp.ServiceReference_HB;
using System.Collections.ObjectModel;
using System.Windows.Data;
using SLWebApp.ChildWindows;

namespace SLWebApp
{
    public partial class FlightInfo : UserControl
    {

        #region properties


        /// <summary>
        /// 登陆用户ID
        /// </summary>
        public int UserID
        {
            set;
            get;
        }


        public string UserType
        {
            get
            {
                if (UserID == 1)
                {
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
        }

        #endregion


        public FlightInfo()
        {
            InitializeComponent();

            System.Windows.Browser.HtmlDocument doc = System.Windows.Browser.HtmlPage.Document;
            if (doc.QueryString.Count > 0)
            {
                userid = doc.QueryString["userid"].ToString();
            }


            if (string.IsNullOrEmpty(userid))
            {
                return;
            }
            int intUserid;
            bool check = Int32.TryParse(userid, out intUserid);
            if (!check)
                return;
            UserID = intUserid;


            InitDate();

            FirstInit(string.Empty);
        }


     


        #region event handler



        /// <summary>
        /// 主航线更改的的时候，此航线跟着联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbmSL_first_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Service.ServiceForSLClient clientServices = new Service.ServiceForSLClient();
            if (e.AddedItems.Count > 0)
            {
                ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                clientServices.GetChildSeaRoutes_ByLineIDAsync(UserID
                    , UserType, item.ID);

                clientServices.GetChildSeaRoutes_ByLineIDCompleted += (s1, e1) =>
                    {
                        ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e1.Result;
                        cmbSL_second.ItemsSource = col;

                    };
            }
        }


        #endregion


        #region event handler

        //新增
        private void btnAdd_VesselSchedule_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ChildWindow_FlightsInfo child = new ChildWindow_FlightsInfo(this);
            child.Show();
        }

        //删除
        private void txtBlockDelete_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ServiceReference_HB.WEB_VM_FLIGHTS selectedModel = dgFlight.SelectedItem as ServiceReference_HB.WEB_VM_FLIGHTS;
            ServiceReference_HB.ServiceForSLClient services = new Service.ServiceForSLClient();
            if (selectedModel == null)
            {
                MessageBox.Show("请选择一行数据！", "提示", MessageBoxButton.OK);
                return;
            }
            else
            {

                MessageBoxResult result = MessageBox.Show("您确定删除该数据吗？", "提示", MessageBoxButton.OKCancel);
                if (result.ToString() == "OK")
                {
                    busyIndicator.IsBusy = true;
                    services.IUDForFlightAsync((int)OperationType.Delete, this.UserID, selectedModel);
                    services.IUDForFlightCompleted += (s1, e1) =>
                    {
                        string outPutStr = e1.Result;
                        if (outPutStr[0] == '0')
                        {

                            MessageBox.Show(outPutStr.Substring(3));
                        }
                        else
                        {
                            PagedCollectionView sources = dgFlight.ItemsSource as PagedCollectionView;
                            sources.Remove(selectedModel);

                        }
                        busyIndicator.IsBusy = false;

                    };

                }

            }
        }

        
        //更新
        private void txtBlockUpdate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            ServiceReference_HB.WEB_VM_FLIGHTS selectedModel = dgFlight.SelectedItem as ServiceReference_HB.WEB_VM_FLIGHTS;
            if (selectedModel == null)
            {
                MessageBox.Show("请选择一行数据！", "提示", MessageBoxButton.OK);
                return;
            }
            else
            {
                ChildWindows.ChildWindow_FlightsInfo child = new ChildWindows.ChildWindow_FlightsInfo(this, OperationType.Update, selectedModel);
                child.Model = selectedModel;
                child.Show();
            }
        }

        private void cmbSL_second_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Service.ServiceForSLClient clientServices = new Service.ServiceForSLClient();
            if (e.AddedItems.Count > 0)
            {
                ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                clientServices.GetTermailAsync(item.ID);
                clientServices.GetTermailCompleted += (s1, e1) =>
                {
                    ObservableCollection<ServiceReference_HB.MainSeaRoutes> lists = e1.Result as ObservableCollection<ServiceReference_HB.MainSeaRoutes>;
                    cmbTerminal.ItemsSource = lists;
                };
            }
        }

        //查询
        private void btnSearch_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            QueryByCondition();
            


        }

        public void QueryByCondition()
        {
            busyIndicator.IsBusy = true;
            string strWhere = string.Empty;

            if (cmbSL_second.SelectedItem != null)
            {
                ServiceReference_HB.MainSeaRoutes seaRoutes = cmbSL_second.SelectedItem as ServiceReference_HB.MainSeaRoutes;
                strWhere = string.Format(" FLT_SLN_ID={0} ", seaRoutes.ID);
            }
            if (cmbTerminal.SelectedItem != null)
            {
                ServiceReference_HB.MainSeaRoutes seaRoute = cmbTerminal.SelectedItem as ServiceReference_HB.MainSeaRoutes;
                strWhere = ProcessStrWhere(strWhere, string.Format(" FLT_TER_CST_ID ={0}", seaRoute.ID));
            }

            FirstInit(strWhere);
        }


      

      
        #endregion




        #region  private methods

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="services"></param>
        private void FirstInit(string strWhere)
        {
            ServiceReference_HB.ServiceForSLClient services = new Service.ServiceForSLClient();
            if (UserType == "0")  //普通用户，取出agentID
            {
                services.GetAgentIDByUserIDAsync(UserID);
                services.GetAgentIDByUserIDCompleted += (s1, e1) =>
                {
                    string agentID = e1.Result;
                    strWhere = ProcessStrWhere(strWhere, string.Format(" AGENT = {0} ", agentID));
                    this.QueryData(strWhere, "sln_name,FLT_SCHDUL, FLT_NO");
                };
            }
            else
            {
                this.QueryData(strWhere, "sln_name,FLT_SCHDUL, FLT_NO");
            }

        }


        private void InitDate()
        {
            Service.ServiceForSLClient clientServices = new Service.ServiceForSLClient();

            //登陆显示名称
            clientServices.GetCustomerNameAsync(UserID);
            clientServices.GetCustomerNameCompleted += (s, e) =>
            {  // txtAgentName.Text = e.Result;
                TXT_AGT_NM.Text = e.Result;
            };


            ///填充主航线
            clientServices.GetMainSeaRoutes_ByUserIdAsync(UserID, UserType);
            clientServices.GetMainSeaRoutes_ByUserIdCompleted += (s, e) =>
            {
                ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
                cbmSL_first.ItemsSource = col;
            };

        }

        private void QueryData(string strWhere, string strOrder)
        {
            Service.ServiceForSLClient clientServices = new Service.ServiceForSLClient();
            clientServices.GetFlightsAsync(strWhere, strOrder);
            clientServices.GetFlightsCompleted += (s, e) =>
                {
                    PagedCollectionView pcv = new PagedCollectionView(e.Result);
                    pcv.PageSize = 10;

                    DataContext = pcv;
                    dgFlight.ItemsSource = pcv;
                    busyIndicator.IsBusy = false;
               
                };
        }

        /// <summary>
        /// 处理sql的查询条件
        /// </summary>
        /// <param name="oldStrWhere"></param>
        /// <param name="newWhere"></param>
        /// <returns></returns>
        private string ProcessStrWhere(string oldStrWhere, string newWhere)
        {
            if (string.IsNullOrEmpty(oldStrWhere))
            {
                return newWhere;
            }
            else
            {
                oldStrWhere += (" and " + newWhere);
            }
            return oldStrWhere;
        }

        #endregion
        #region  private fields


        string userid = string.Empty;




        #endregion

    
    

    }
}
